// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 .
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : xxx_monitor.sv
// Author        : 
// Created On    : 2022-08-25 15:47
// Last Modified : 2023-12-01 10:42 by ICer
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

`ifndef __XXX_MONITOR_SV__
`define __XXX_MONITOR_SV__

class xxx_monitor extends uvm_monitor;
	virtual xxx_interface vif;
	uvm_analysis_port #(xxx_transaction) ap;
	
	`uvm_component_utils(xxx_monitor)
	
	extern function new(string name = "xxx_monitor", uvm_component parent=null);
	extern virtual function void build_phase(uvm_phase phase);
	extern virtual task reset_phase(uvm_phase phase);
	extern virtual task main_phase(uvm_phase phase);
	extern virtual task receive_pkt();
endclass: xxx_monitor

function xxx_monitor::new(string name = "xxx_monitor", uvm_component parent=null);
	super.new(name, parent);
endfunction: new

function void xxx_monitor::build_phase(uvm_phase phase);
	super.build_phase(phase);
	`uvm_info("xxx_monitor", "build_phase is called", UVM_HIGH);
	if(!uvm_config_db #(virtual xxx_interface)::get(this, "", "vif", vif))begin
		`uvm_fatal("xxx_monitor", "virtual interface get fatal");
	end
	ap   = new("ap", this);
endfunction: build_phase

task xxx_monitor::reset_phase(uvm_phase phase);
  phase.raise_objection(this);
  wait(this.vif.rst_n === 1'b0);
  wait(this.vif.rst_n === 1'b1);
  phase.drop_objection(this);
endtask: reset_phase


task xxx_monitor::main_phase(uvm_phase phase);
	fork 
		this.receive_pkt();
	join_none
endtask: main_phase

task xxx_monitor::receive_pkt();
	xxx_transaction data = new();
	while(1) begin
		@this.vif.mon;
		if(this.vif.mon.valid & this.vif.mon.ready)begin
            data.data = this.vif.mon.data;
            data.crc = this.vif.mon.crc;
            data.last = this.vif.mon.last;
			data.unpack();
			ap.write(data);
		end
	end
endtask: receive_pkt

`endif
